package com.dst.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dst.entity.AuctionRecord;
import com.dst.entity.User;
import com.dst.service.FeignService;
import com.dst.service.IAuctionRecordService;
import com.dst.utli.JwtUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董胜涛
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/auctionRecord")
public class AuctionRecordController {
  @Resource
  private FeignService feignService;
  @Resource
  private IAuctionRecordService iAuctionRecordService;
@GetMapping("/selectById/{id}")
  public List<AuctionRecord> selectAll(@PathVariable("id") Integer id){
  QueryWrapper<AuctionRecord> wrapper = new QueryWrapper<>();
  wrapper.eq("auction_id",id);
  wrapper.orderByDesc("record_price");
  List<AuctionRecord> list = iAuctionRecordService.list(wrapper);
  for (AuctionRecord ar:list
       ) {
    Integer userId = ar.getUserId();
    User user = feignService.selectOne(userId);
    ar.setUser(user);
  }
  return list;
}
@PutMapping("/outPrice")
  public void outPrice(@RequestHeader("authorization") String jwt,@RequestBody AuctionRecord auctionRecord){
  DecodedJWT verify = JwtUtils.verify(jwt);
  String idstr = verify.getClaim("id").asString();
  int id = Integer.parseInt(idstr);
  auctionRecord.setUserId(id);
  auctionRecord.setRecordTime(LocalDateTime.now());
  iAuctionRecordService.chekPrice(auctionRecord);
}
}

